import 'dart:convert';

T? asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class Cart {
  Cart({
    this.cartId,
    this.goodsId,
    this.name,
    this.price,
    this.number,
  });

  factory Cart.fromJson(Map<String, dynamic> jsonRes) => Cart(
        cartId: asT<String?>(jsonRes['cartId']),
        goodsId: asT<String?>(jsonRes['goodsId']),
        name: asT<String?>(jsonRes['name']),
        price: asT<double?>(jsonRes['price']),
        number: asT<int?>(jsonRes['number']),
      );

  String? cartId;
  String? goodsId;
  String? name;
  double? price;
  int? number;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'cartId': cartId,
        'goodsId': goodsId,
        'name': name,
        'price': price,
        'number': number,
      };

  Cart clone() =>
      Cart.fromJson(asT<Map<String, dynamic>>(jsonDecode(jsonEncode(this)))!);
}
